// miniprogram/pages/index.js
import {
  getSolitaireList,
  delSolitaire
} from './../../api/solitaire.js'
const dayjs = require('dayjs')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [

    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getSolitaireList().then(res => {
      console.log('list', res)
      this.handlerSolitaireList(res)
    })
  },
  onShareAppMessage(e) {
    console.log(e)
    const id =e.target.dataset.id
    const title =e.target.dataset.title
    return {
      title: title,
      imageUrl: '/images/share.png',
      path:"/pages/deital/deital?_id="+id
    }
  },
  handlerSolitaireList(res) {

    const changeData = {}
    let index = this.data.list.length
    res.result.list.forEach(item => {
      changeData['list[' + (index++) + ']'] = {
        _id: item._id,
        head: item.user.avatarUrl, //头像
        name: item.user.nickName, //发起人
        time: dayjs(item.date).format('YYYY-MM-DD HH:mm'), //时间
        typeName: item.activity.name,
        typeColor: item.activity.color,
        title: item.title,
        desc: item.remarks,
        num: item.num, //人数
        btn: "./../../images/btn-del.png", //删除
        isShowBtn: item.user.isSelf,
        isIndex: true,
      }
    })
    this.setData(changeData)
  },
  handleContact(e) {
    //客服
    console.log(e)
  },
  handlerClick(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: './../deital/deital?_id=' + id,
    })
  },
  handlerRightBtn(e) {
    console.log("right", e)
    const index = e.currentTarget.dataset.index
    wx.showModal({
      content: "确认删除?"
    }).then(res => {
      if (res.confirm) {
        const _id = this.data.list[index]._id
        wx.showLoading({
          title: '删除中...',
        })
        return delSolitaire(_id).then(res => {
          console.log("del", res)
          wx.hideLoading()
          this.data.list.splice(index, 1)
          this.setData({
            list: this.data.list
          })
        })
      }
    })
  }

})